#include "single_linklist.h"

SingleLinkList SingleLinkListCreateHead()
{
    SingleLinkList list = (SingleLinkList)malloc(sizeof(SingleLinkListNode));
	SingleLinkListNode * node;
	ElemType data;
    scanf("%d",&data);
    while(data != 0)
    {
        node = (SingleLinkList)malloc(sizeof(SingleLinkListNode));
        node->data = data;
        node->next = list->next;
        list->next = node;
        scanf("%d",&data);
    }
    return list;
}

void SingleLinkListTraverse(SingleLinkList list)
{
    SingleLinkList temp =  list->next;
    while(temp != NULL)
    {
        printf("%d\n",temp->data);
        temp = temp->next;
    }
}

SingleLinkList SingleLinkListCreateTail()
{
    SingleLinkList head = (SingleLinkList)malloc(sizeof(SingleLinkListNode));
    SingleLinkListNode * node;
    SingleLinkListNode * tail = head; // head本身没有存数据
    ElemType data;
    scanf("%d",&data);
    while(data != 0)
    {
        node = (SingleLinkList)malloc(sizeof(SingleLinkListNode));
        node->data = data;
        tail->next = node;
        tail = node;
        scanf("%d",&data);
    }
    tail->next = NULL;

    return head;
}

SingleLinkListNode *SingleLinkListGetnodeNum(SingleLinkList list,int num)
{
    int count = 1; // 使获取位置以1开始，而不是0，10 12 12中10使第一个数据而不是第0个
    SingleLinkListNode * temp = list->next;
    if(num == 0)
        return temp;
    if(num < 0)
        return NULL;

    while(temp != NULL && count < num)
    {
        temp = temp->next;
        count++;
    }
    return temp;
}

SingleLinkListNode *SingleLinkListGetnodeData(SingleLinkList list, ElemType data)
{
    SingleLinkListNode * temp = list->next;
    while(temp!= NULL && temp->data != data)
    {
        temp = temp->next;
    }
    return temp;
}


void SingleLinkListInsert(SingleLinkList list, int pos, ElemType data)
{
    /*1.检查插入位置的合法性*/
    /*2.选择前插或者后插*/

    SingleLinkListNode * temp = SingleLinkListGetnodeNum(list,--pos); // --pos 先自减然后传递
    printf("位置不合法!\n");
    SingleLinkList node = (SingleLinkList)malloc(sizeof(SingleLinkListNode));
    node->data = data;
    node->next = temp->next;
    temp->next = node;
}

SingleLinkList SingleLinkListGenerate()
{
    SingleLinkList head = (SingleLinkList)malloc(sizeof(SingleLinkListNode));
    SingleLinkListNode * node;
    SingleLinkListNode * tail = head;
    ElemType data = 100;
    int i=0;
    while(i < 10)
    {
        node = (SingleLinkList)malloc(sizeof(SingleLinkListNode));
        node->data = data;
        tail->next = node;
        tail = node;
        i++;
        data++;
    }
    return head;
}

void SingleLinkListAppend(SingleLinkList list, ElemType data)
{
    SingleLinkList tail;
    SingleLinkList node= (SingleLinkList)malloc(sizeof(SingleLinkListNode));
    tail = SingleLinkListGetTailNode(list);
    /*验证tail*/
//    printf("tail=%d\n",tail->data);
    node->data = data;
    tail->next = node;
    tail = node;
}

SingleLinkListNode *SingleLinkListGetTailNode(SingleLinkList list)
{
    SingleLinkList head = (SingleLinkList)malloc(sizeof(SingleLinkListNode));
    head = list;
    while(head->next != NULL)
        head = head->next;
    return head;
}

void SingleLinkListDeleteNode(SingleLinkList list, int pos)
{
    SingleLinkListNode * node = SingleLinkListGetnodeNum(list,--pos);
    printf("tail=%d\n",node->data);

    /*以下代码功能等同 node->next = node->next->next; 只是内存没有释放*/
    SingleLinkListNode * temp = node->next;
    node->next = temp->next;
    free(temp);
}

int SingleLinkListGetListLength(SingleLinkList list)
{
    SingleLinkListNode * node = list;
    int length = 0;
    while(node ->next != NULL){
        length++;
        node = node->next;
    }
    return length;
}
